// 移除顶部导入的jest
import api from '../api'
// 使用jest.mock的自动提升特性
jest.mock('../stores/user', () => ({
  useUserStore: () => ({
    library_users: { userId: 123 },
    token: 'mock_token'
  })
}));

jest.mock('../api', () => ({
  book: {
    createBorrow: jest.fn(),
    userReturnBook: jest.fn()
  }
}));

describe('图书借阅功能测试套件', () => {
  beforeEach(() => {
    jest.clearAllMocks()
  })

  test('正常借阅流程', async () => {
    // 模拟借阅接口成功响应
    api.book.createBorrow.mockResolvedValue({ code: 200 })

    const params = {
      bookId: 1,
      borrowDate: '2024-03-01',
      returnDate: '2024-03-15'
    }

    const response = await api.book.createBorrow(params)
    
    expect(api.book.createBorrow).toHaveBeenCalledWith({
      userId: 123,
      ...params
    })
    expect(response.code).toBe(200)
  })

  test('借阅日期校验失败', async () => {
    const params = {
      bookId: 1,
      borrowDate: '2024-03-15', // 错误日期
      returnDate: '2024-03-01'  // 早于借阅日期
    }

    await expect(api.book.createBorrow(params)).rejects.toThrow()
  })
})

describe('图书归还功能测试套件', () => {
  test('正常归还流程', async () => {
    api.book.userReturnBook.mockResolvedValue({ code: 200 })
    
    const response = await api.book.userReturnBook({ recordId: 456 })
    
    expect(api.book.userReturnBook).toHaveBeenCalledWith({
      recordId: 456
    })
    expect(response.code).toBe(200)
  })

  test('归还他人记录', async () => {
    // 需要根据实际接口错误码调整
    api.book.userReturnBook.mockResolvedValue({ 
      code: 403,
      message: '无操作权限'
    })
    
    const response = await api.book.userReturnBook({ recordId: 999 })
    expect(response.code).toBe(403)
  })
})